﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    public class LevelEditor : MonoBehaviour
    {
        [CompilerGenerated]
        private static Action <>f__am$cache8;
        [CompilerGenerated]
        private static Action <>f__am$cache9;
        [CompilerGenerated]
        private static Func<Level, bool> <>f__am$cacheA;
        [CompilerGenerated]
        private static Func<Level, string> <>f__am$cacheB;
        [CompilerGenerated]
        private static Action<float> <>f__am$cacheC;
        private static Level m_curLevel;
        private static LevelEditor m_Instance;
        [SerializeField]
        private GameObject m_networkManagerPrefab;
        private static string m_selectedPrefabName = string.Empty;

        public event Action onLoadLevel;

        public event Action onSelectPrefab;

        public LevelEditor()
        {
            if (<>f__am$cache8 == null)
            {
                <>f__am$cache8 = new Action(LevelEditor.<onSelectPrefab>m__0);
            }
            this.onSelectPrefab = <>f__am$cache8;
            if (<>f__am$cache9 == null)
            {
                <>f__am$cache9 = new Action(LevelEditor.<onLoadLevel>m__1);
            }
            this.onLoadLevel = <>f__am$cache9;
        }

        [CompilerGenerated]
        private static void <onLoadLevel>m__1()
        {
        }

        [CompilerGenerated]
        private static void <onSelectPrefab>m__0()
        {
        }

        public void AddLevel()
        {
            this.AddLevel(null);
        }

        public void AddLevel(string levelName)
        {
        }

        private void Awake()
        {
            this.levels = Level.GetAllLevels().ToList<Level>();
            isSimulating = false;
            m_selectedPrefabName = PlayerPrefs.GetString("LevelEditor.selectedPrefabName");
            if (<>f__am$cacheA == null)
            {
                <>f__am$cacheA = x => x.name == curLevelName;
            }
            this.curLevel = this.levels.FirstOrDefault<Level>(<>f__am$cacheA);
            if (this.curLevel != null)
            {
                this.LoadLevel(this.curLevel.name);
            }
        }

        public void Erase(Vector3 position)
        {
            this.Erase(this.selectedPrefabName, position);
        }

        public void Erase(string prefabName, Vector3 position)
        {
            if ((this.curLevel != null) && (prefabName != null))
            {
                this.curLevel.RemoveInstance(prefabName, position);
            }
        }

        public IEnumerable<Instance> GetInstances(Vector3 position)
        {
            return this.curLevel.GetInstances(position);
        }

        public IEnumerable<string> GetLevelNames()
        {
            if (<>f__am$cacheB == null)
            {
                <>f__am$cacheB = x => x.name;
            }
            return Level.GetAllLevels().Select<Level, string>(<>f__am$cacheB);
        }

        public void LoadLevel(string levelName)
        {
            Level level = Level.GetLevel(levelName);
            if (level != null)
            {
                if ((this.curLevel != null) && this.curLevel.isLoaded)
                {
                    this.curLevel.Save();
                    this.curLevel.Unload();
                }
                this.curLevel = level;
                if (this.curLevel != null)
                {
                    this.StartWaitForCoroutine<Coroutine>(delegate {
                        if (<>f__am$cacheC == null)
                        {
                            <>f__am$cacheC = p => UIProgressBar.Show("Loading...", p);
                        }
                        return this.curLevel.Load(<>f__am$cacheC);
                    }, delegate {
                        UIProgressBar.Clear();
                        foreach (Instance instance in this.curLevel.GetInstances())
                        {
                            if (instance != null)
                            {
                                this.SetKinematic(instance);
                            }
                        }
                        this.onLoadLevel();
                    });
                }
            }
        }

        private void OnDestroy()
        {
            PlayerPrefs.SetString("LevelEditor.selectedPrefabName", this.selectedPrefabName);
        }

        private void OnDisable()
        {
            this.SaveCurrentLevel();
        }

        public Instance Paint(Vector3 position)
        {
            return this.Paint(this.selectedPrefabName, position);
        }

        public Instance Paint(string prefabName, Vector3 position)
        {
            if ((this.curLevel == null) || (prefabName == null))
            {
                return null;
            }
            if (AssetManager.IsEffect(prefabName))
            {
                return null;
            }
            Instance ins = this.curLevel.AddInstance(prefabName, position);
            this.SetKinematic(ins);
            return ins;
        }

        public void ReloadLevel()
        {
            if ((this.curLevel != null) && !this.isLoading)
            {
                this.LoadLevel(this.curLevel.name);
            }
        }

        public void SaveCurrentLevel()
        {
            if (this.curLevel != null)
            {
                this.curLevel.Save();
            }
        }

        private void SetKinematic(Instance ins)
        {
            if ((ins != null) && (ins.transform != null))
            {
                Rigidbody component = ins.transform.GetComponent<Rigidbody>();
                if (component != null)
                {
                    component.isKinematic = true;
                }
            }
        }

        public void Simulate()
        {
            isSimulating = true;
            UnityEngine.Object.Instantiate<GameObject>(this.m_networkManagerPrefab);
            NetworkUtility.StartHost();
            IngameScene.Load(curLevelName);
        }

        public static Vector3 Snap(Vector3 position)
        {
            return new Vector3(Mathf.Round(position.x), 0f, Mathf.Round(position.z));
        }

        private void Start()
        {
        }

        public Level curLevel
        {
            get
            {
                return m_curLevel;
            }
            private set
            {
                m_curLevel = value;
                if (value != null)
                {
                    curLevelName = value.name;
                }
            }
        }

        public static string curLevelName
        {
            get
            {
                return PlayerPrefs.GetString("LevelEditor.curLevelName");
            }
            set
            {
                PlayerPrefs.SetString("LevelEditor.curLevelName", value);
            }
        }

        public bool hasLevelSelected
        {
            get
            {
                return (this.curLevel != null);
            }
        }

        public static LevelEditor Instance
        {
            get
            {
                return ((m_Instance == null) ? (m_Instance = UnityEngine.Object.FindObjectOfType<LevelEditor>()) : m_Instance);
            }
        }

        public bool isLoading
        {
            get
            {
                return ((this.curLevel != null) && !this.curLevel.isLoaded);
            }
        }

        public static bool isSimulating
        {
            [CompilerGenerated]
            get
            {
                return <isSimulating>k__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                <isSimulating>k__BackingField = value;
            }
        }

        public List<Level> levels { get; private set; }

        public string selectedPrefabName
        {
            get
            {
                return m_selectedPrefabName;
            }
            set
            {
                m_selectedPrefabName = value;
                if (value != null)
                {
                    this.onSelectPrefab();
                }
            }
        }
    }
}

